#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 105;
const int INF = 100005;
int n, a[N][N], d[N], vis[N] = {false};
int prim () {
	 d[1] = 0;
	 int ans = 0;
	 for (int i = 1; i <= n; i++) {
	 	int u = -1, MIN = INF;
	 	for (int j = 1; j <= n; j++) {
	 		if (!vis[j] && d[j] < MIN) {
	 			u = j;
	 			MIN = d[j];
	 		}
	 	}
	 	if (u == -1) {
	 		return -1;
	 	}
	 	vis[u] = 1;
	 	ans += d[u];
	 	for (int j = 1; j <= n; j++) {
	 		if (!vis[j] && a[u][j] != INF && a[u][j] < d[j]) {
	 			d[j] = a[u][j];
	 		}
	 	}
	 }
	 return ans;
}
int main() {
	scanf ("%d", &n);
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			scanf ("%d", &a[i][j]);
			if (a[i][j] == 0) {
				a[i][j] = INF;
			}
		}
	}
	for (int i = 1; i <= n; i++) {
		d[i] = INF;
	}
	int ans = prim();
	printf ("%d\n", ans);
	return 0;
}